﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class AdminCustomerController : Controller
    {
        //
        // GET: /AdminCustomer/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
            IEnumerable<AdminCustomerModels> _AdminCustomers = _AdminCustomerRepo.GetAdminCustomer();
            return View(_AdminCustomers);
        }

        //
        // GET: /AdminCustomer/Details/5

        public ActionResult Details(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
            AdminCustomerModels _AdminCustomer = new AdminCustomerModels();
            _AdminCustomer = _AdminCustomerRepo.GetAdminCustomerId(id);
            return View(_AdminCustomer);
        }

        //
        // GET: /AdminCustomer/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            AdminCustomerModels AdminCustomer = new AdminCustomerModels();

            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M001";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode + '-' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };
            AdminCustomer.LocSetSelect = selectlist;
            return View(AdminCustomer);
        } 

        //
        // POST: /AdminCustomer/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
                AdminCustomerModels _AdminCustomer = new AdminCustomerModels();

                _AdminCustomer.LocationSetCode = collection["LocationSetCode"];
                _AdminCustomer.UserName = (!string.IsNullOrEmpty(collection["UserName"])) ? collection["UserName"] : "";
                _AdminCustomer.Password = (!string.IsNullOrEmpty(collection["Password"])) ? collection["Password"] : "";
                _AdminCustomer.CodeConfirm = (!string.IsNullOrEmpty(collection["CodeConfirm"])) ? collection["CodeConfirm"] : "";
                _AdminCustomer.NameTh = collection["NameTh"];
                _AdminCustomer.NameEng = collection["NameEng"];
                _AdminCustomer.CreateBy = (string)Session["User"]; 

                 if (_AdminCustomer.CodeConfirm == "")
                {
                    TempData["errorname0"] = "ยังไม่ป้อนข้อมูล Confirm Code";
                    return RedirectToAction("Create");
                }
                 else if (_AdminCustomer.NameTh == "")
                 {
                     TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อภาษาไทย";
                     return RedirectToAction("Create");
                 }else if (_AdminCustomer.NameEng == "")
                  {
                    TempData["errorname2"] = "ยังไม่ป้อนข้อมูลชื่อภาษาอังกฤษ";
                    return RedirectToAction("Create");
                  }
                
                  else
                  {
                      bool result = _AdminCustomerRepo.Create(_AdminCustomer);
                      return RedirectToAction("Index");
                  }
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /AdminCustomer/Edit/5
 
        public ActionResult Edit(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
            AdminCustomerModels _AdminCustomer = new AdminCustomerModels();
            _AdminCustomer = _AdminCustomerRepo.GetAdminCustomerId(id);
            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M001";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode + '-' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };
            _AdminCustomer.LocSetSelect = selectlist;
            return View(_AdminCustomer);
        }

        //
        // POST: /AdminCustomer/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
                AdminCustomerModels _AdminCustomer = new AdminCustomerModels();
                _AdminCustomer.AdminId = id;
                _AdminCustomer.LocationSetCode = collection["LocationSetCode"];
                _AdminCustomer.UserName = (!string.IsNullOrEmpty(collection["UserName"]))? collection["UserName"]:"";
                _AdminCustomer.Password = (!string.IsNullOrEmpty(collection["Password"]))?collection["Password"]:"";
                _AdminCustomer.CodeConfirm = (!string.IsNullOrEmpty(collection["CodeConfirm"])) ? collection["CodeConfirm"] : "";
                _AdminCustomer.NameTh = collection["NameTh"];
                _AdminCustomer.NameEng = collection["NameEng"];
                _AdminCustomer.UpdateBy = (string)Session["User"];
                if (_AdminCustomer.CodeConfirm == "")
                {
                    TempData["errorname0"] = "ยังไม่ป้อนข้อมูล Confirm Code";
                    return RedirectToAction("Edit");
                }
                else if (_AdminCustomer.NameTh == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อภาษาไทย";
                    return RedirectToAction("Edit");
                }
                else if (_AdminCustomer.NameEng == "")
                  {
                    TempData["errorname2"] = "ยังไม่ป้อนข้อมูลชื่อภาษาอังกฤษ";
                    return RedirectToAction("Edit");
                  }
                 else
                 {
                      bool result = _AdminCustomerRepo.Edit(_AdminCustomer);

                      return RedirectToAction("Index");
                  }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminCustomer/Delete/5

        public ActionResult Delete(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
            AdminCustomerModels _AdminCustomer = new AdminCustomerModels();
            _AdminCustomer = _AdminCustomerRepo.GetAdminCustomerId(id);

            return View(_AdminCustomer);
        }

        //
        // POST: /AdminCustomer/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                AdminCustomerRepo _AdminCustomerRepo = new AdminCustomerRepo();
                string _userid = (string)Session["User"];
                AdminCustomerModels _AdminCustomer = new AdminCustomerModels();
                _AdminCustomer.AdminId = id;
                _AdminCustomer.UpdateBy = (string)Session["User"];
                bool result = _AdminCustomerRepo.Delete(_AdminCustomer);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
